let {
  generateKeyPairSync,
  createSign,
  createVerify
} = require('crypto');
// 签名
let passphrase = 'zhufeng';
let rsa = generateKeyPairSync('rsa', {
  modulusLength: 1024,
  publicKeyEncoding: {
    type: 'spki',
    format: 'pem'
  },
  privateKeyEncoding: {
    type: 'pkcs8',
    format: 'pem',
    cipher: 'aes-256-cbc',
    passphrase
  }
});
let content = 'hello';
const sign = getSign(content, rsa.privateKey, passphrase);
let serverCertIsValid = verifySign(content, sign, rsa.publicKey);
console.log('serverCertIsValid', serverCertIsValid);

function getSign(content, privateKey, passphrase) {
  var sign = createSign('RSA-SHA256');
  sign.update(content);
  return sign.sign({
    key: privateKey,
    format: 'pem',
    passphrase
  }, 'hex');
}

function verifySign(content, sign, publicKey) {
  var verify = createVerify('RSA-SHA256');
  verify.update(content);
  return verify.verify(publicKey, sign, 'hex');
}